#include "widget.h"
#include "ui_widget.h"
#include <QDebug>
#include <QRegExpValidator>
Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    //设置姓名输入框
    ui->lineEdit_name->setFrame(true);
    ui->lineEdit_name->setPlaceholderText("请输入姓名");
    ui->lineEdit_name->setClearButtonEnabled(true);

    //设置密码输入框
    ui->lineEdit_password->setFrame(true);
    ui->lineEdit_password->setPlaceholderText("请输入密码");
    ui->lineEdit_password->setClearButtonEnabled(true);

    //设置密码二次输入
    ui->lineEdit_passwordAgain->setFrame(true);
    ui->lineEdit_passwordAgain->setPlaceholderText("请再次输入密码");
    ui->lineEdit_passwordAgain->setClearButtonEnabled(true);
    //设置密码隐藏
    ui->lineEdit_password->setEchoMode(QLineEdit::Password);
    ui->lineEdit_passwordAgain->setEchoMode(QLineEdit::Password);

    //设置性别
    //设置电话
    ui->lineEdit_phone->setFrame(true);
    ui->lineEdit_phone->setPlaceholderText("请输入电话");
    ui->lineEdit_phone->setClearButtonEnabled(true);
    //电话有格式限制
    //ui->lineEdit_phone->setInputMask("000-0000-0000");
    QRegExp regexp(R"(^1\d{10}$)");
    ui->lineEdit_phone->setValidator(new QRegExpValidator(regexp));


}

Widget::~Widget()
{
    delete ui;
}

void Widget::Compare()
{
    QString s1 = ui->lineEdit_password->text();
    QString s2 = ui->lineEdit_passwordAgain->text();
    if(s1.isEmpty() && s2.isEmpty())
    {
        ui->label_dispaly->setText("您输入的密码为空");
        return;
    }
    if(s1 == s2)
    {
        ui->label_dispaly->setText("输入的密码一致");
    }
    else
    {
        ui->label_dispaly->setText("输入的密码不一致");
    }
    return;
}


void Widget::on_pushButton_submit_clicked()
{
    QString gender = ui->radioButton_male->isChecked()?"男":"女";
    qDebug() << "姓名: " << ui->lineEdit_name->text()
             << "密码: " << ui->lineEdit_password->text()
             << "性别: " << gender
             << "电话: " << ui->lineEdit_phone->text();
}
//当输入框中输入内容后，触发信号的槽函数
void Widget::on_lineEdit_phone_textEdited(const QString &arg1)
{
    //在槽函数中验证输入内容和正则表达式是否匹配成功
    QString text = arg1;
    int pos;//第二个参数是输出型参数，当匹配失败的时候，返回从哪个位置开始匹配失败的
    if(ui->lineEdit_phone->validator()->validate(text,pos) ==QValidator::Acceptable )
    {
        ui->pushButton_submit->setEnabled(true);
    }
    else
        ui->pushButton_submit->setEnabled(false);

}

void Widget::on_lineEdit_password_textEdited(const QString &arg1)
{
 (void)arg1;
    this->Compare();
}

void Widget::on_lineEdit_passwordAgain_textEdited(const QString &arg1)
{
    (void)arg1;
    this->Compare();
}

void Widget::on_checkBox_toggled(bool checked)
{
    if(checked)
    {
        ui->lineEdit_password->setEchoMode(QLineEdit::Normal);
        ui->lineEdit_passwordAgain->setEchoMode(QLineEdit::Normal);
    }
    else
    {
        ui->lineEdit_password->setEchoMode(QLineEdit::Password);
        ui->lineEdit_passwordAgain->setEchoMode(QLineEdit::Password);

    }
}
